import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.QueryDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.io.StringWriter;

/**
 * @author BG343674
 * created by BG343674 on 2020/4/14
 */
public class QueryDataFromDB {


    public static void main(String[] args) throws Exception {
        StringWriter stringWriter = new StringWriter();
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        DriverManagerDataSource dataSource = (DriverManagerDataSource) ctx.getBean("dataSource");
        IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
        QueryDataSet partialDataSet = new QueryDataSet(connection);
        partialDataSet.addTable("dianjia_rms_purchaseorder", "SELECT * FROM dianjia_rms_purchaseorder");
        partialDataSet.addTable("dianjia_rms_purchaseorderitem", "SELECT * FROM dianjia_rms_purchaseorderitem");
        partialDataSet.addTable("dianjia_rms_storestock", "SELECT * FROM dianjia_rms_storestock");
        FlatXmlDataSet.write(partialDataSet, stringWriter, "utf-8");
        System.out.println(stringWriter.getBuffer().toString());


    }


}
